import threading
import wx
from socket import socket, AF_INET, SOCK_STREAM

class ChatClient(wx.Frame):
    def __init__(self, client_name):
        wx.Frame.__init__(self, None, id=1001, title=f'{client_name}客户端', size=(400, 450))
        pl = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)
        fgz1 = wx.FlexGridSizer(wx.HORIZONTAL)

        conn_btn = wx.Button(pl, label='连接')
        dis_btn = wx.Button(pl, label='断开')

        fgz1.Add(conn_btn, 1, wx.TOP | wx.LEFT)
        fgz1.Add(dis_btn, 1, wx.TOP | wx.RIGHT)
        box.Add(fgz1, 1, wx.ALIGN_CENTER)

        self.show_text = wx.TextCtrl(pl, size=(400, 210), style=wx.TE_MULTILINE | wx.TE_READONLY)
        box.Add(self.show_text, 1, wx.EXPAND | wx.ALL)

        self.chat_text = wx.TextCtrl(pl, size=(400, 120), style=wx.TE_MULTILINE)
        box.Add(self.chat_text, 1, wx.EXPAND | wx.ALL)

        reset_btn = wx.Button(pl, label='重置')
        send_btn = wx.Button(pl, label='发送')

        fgz2 = wx.FlexGridSizer(wx.HORIZONTAL)
        fgz2.Add(reset_btn, 1, wx.TOP | wx.LEFT)
        fgz2.Add(send_btn, 1, wx.TOP | wx.RIGHT)
        box.Add(fgz2, 1, wx.ALIGN_CENTER)

        pl.SetSizer(box)

        '''以上代码为界面绘制'''

        self.Bind(wx.EVT_BUTTON, self.connect_to_server, conn_btn)
        self.Bind(wx.EVT_BUTTON, self.dis_conn_server, dis_btn)
        self.Bind(wx.EVT_BUTTON, self.send_to_server, send_btn)
        self.Bind(wx.EVT_BUTTON, self.reset, reset_btn)

        self.client_name = client_name
        self.isConnected = False
        self.client_socket = None

    def dis_conn_server(self, event):
        self.client_socket.send('quit'.encode('utf-8'))
        self.isConnected = False

    def reset(self, event):
        self.chat_text.Clear()

    def send_to_server(self, event):
        if self.isConnected:
            data = self.chat_text.GetValue()
            if data:
                self.client_socket.send(data.encode('utf-8'))
                self.chat_text.Clear()


    def connect_to_server(self, event):
        print('连接服务器')
        if not self.isConnected:
            server_host_port = ('127.0.0.1', 8888)
            self.client_socket = socket(AF_INET, SOCK_STREAM)
            self.client_socket.connect(server_host_port)
            self.client_socket.send(self.client_name.encode('utf-8'))

            client_thread = threading.Thread(target=self.recv_data)
            client_thread.deamon = True
            self.isConnected = True
            client_thread.start()

    def recv_data(self):
        while self.isConnected:
            data = self.client_socket.recv(1024).decode('utf-8')
            self.show_text.AppendText('-'* 40 + '\n' +data + '\n')


if __name__ == '__main__':
    app = wx.App()
    client = ChatClient('jack')

    client.Show()
    app.MainLoop()

